package net.neimeng.luculent.commonmodule.viewmodel;

import android.app.Application;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;

import net.neimeng.luculent.commonmodule.entities.User;
import net.neimeng.luculent.commonmodule.service.PersonalService;
import net.neimeng.luculent.coremodel.base.viewmodel.BaseViewModel;
import net.neimeng.luculent.coremodel.datamodel.http.MapResult;
import net.neimeng.luculent.coremodel.datamodel.http.RXObserver;
import net.neimeng.luculent.coremodel.datamodel.http.Result;
import net.neimeng.luculent.coremodel.datamodel.http.RetrofitUtils;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class LoginViewModel extends BaseViewModel {

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    private PersonalService personalService = RetrofitUtils.getService(PersonalService.class);

    //获取验证码 是否成功的标识
    private MutableLiveData<Result> code = new MutableLiveData<>();

    //登录 是否成功的标识
    private MutableLiveData<MapResult<User>> login = new MutableLiveData<>();

    public MutableLiveData<Result> getCode() {
        return code;
    }

    public MutableLiveData<MapResult<User>> getLogin() {
        return login;
    }

    //获取验证码
    public void getCode(String cellphoneNo){
        Observable<Result> observable =  personalService.getLoginCode(cellphoneNo);
        observable.compose(this.<Result>compose())
                .subscribe(new RXObserver<Result>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }
                    @Override
                    public void onNext(Result result) {
                         code.setValue(result);
                    }

                });
    }

    //登录
    public void login(String cellphoneNo , String code ,String cId){
        Observable<MapResult<User>> observable = personalService.login(cellphoneNo,code,cId);
        observable.compose(this.<MapResult<User>>compose())
                .subscribe(new RXObserver<MapResult<User>>() {
                    @Override
                    public void onSubscribe(Disposable disposable) {
                        addDisposable(disposable);
                    }
                    @Override
                    public void onNext(MapResult<User> result) {
                        login.setValue(result);
                    }

                });
    }


}
